package com.zhongge.collection;

import java.net.CookieHandler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @ClassName TraverseTest
 * @Description TODO traverse是遍历的意思
 * @Author 李明忠
 * @Date 2025/6/13 20:15
 * @Version 1.0
 */
public class TraverseTest {
    public static void main(String[] args) {
        //创建一个容器
        Collection c = new ArrayList();

        //添加元素
        c.add(1);
        c.add("李华");
        c.add("刘备");
        c.add("张飞");
        c.add("关羽");

        //遍历方式1：转为数组遍历
        Object[] o = c.toArray();
        for (int i = 0; i < o.length; i++) {
            System.out.print(o[i] + " ");
        }
        System.out.println();

        //遍历方式2：使用增强for循环
        for(Object tmp : c) {
            System.out.print(tmp + " ");
        }
        System.out.println();

        //遍历方式3：使用迭代器
        Iterator iterator = c.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
}
